package com.aind.common.context;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lpeng on 2017/2/27.
 */
public class ThreadLocalUtil {

    private static final ThreadLocal<Map<Object, Object>> THREAD_LOCAL = new ThreadLocal<Map<Object, Object>>();

    /**
     * 向threadlocal中放值
     * @param key
     * @param value
     */
    public static void set(Object key, Object value){
        Map<Object, Object> map = THREAD_LOCAL.get();
        if (null == map){
            map = new HashMap<Object, Object>();
            map.put(key, value);
            THREAD_LOCAL.set(map);
        } else {
            map.put(key, value);
        }
    }

    /**
     * 从threadlocal中取值
     * @param key
     * @return
     */
    public static Object get(Object key){
        Object obj = null;
        Map<Object, Object> map = THREAD_LOCAL.get();
        if (null != map){
            obj = map.get(key);
        }
        return obj;
    }
}
